package com.neu.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableRowSorter;

import com.neu.dao.impl.PatientDao;
import com.neu.pojo.Patient;
import com.neu.pojo.Worker;
import com.neu.table.PatientTable;

import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;

public class WorkerUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTable table;
	private PatientTable patientTable = new PatientTable();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					WorkerUI frame = new WorkerUI(new Worker());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public WorkerUI(Worker w) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 801, 503);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(27, 25, 188, 32);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("姓名查询");
		btnNewButton.setBounds(259, 25, 128, 27);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				TableRowSorter sorter = new TableRowSorter(patientTable);
				table.setRowSorter(sorter);
			    String s = textField.getText();
			    
			    sorter.setRowFilter(RowFilter.regexFilter(s));
			}
		});
		
		JButton btnNewButton_1 = new JButton("修改个人信息");
		btnNewButton_1.setBounds(631, 84, 146, 23);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				WorkerChangeInformationUI workerChangeInformationUI = new WorkerChangeInformationUI(w);
				workerChangeInformationUI.setVisible(true);
				setVisible(false);
			}
		});
		
	  	JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.setBounds(27, 132, 97, 23);
		contentPane.add(btnNewButton_2);
		btnNewButton_2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(patientTable.delete() == 0) {
					JOptionPane.showMessageDialog(null, "请选中删除的对象");
				}else {
					patientTable.fireTableDataChanged();
					JOptionPane.showMessageDialog(null, "删除成功");
				}
			}	
		});
		
		JButton btnNewButton_2_1 = new JButton("模板列表");
		btnNewButton_2_1.setBounds(555, 132, 97, 23);
		contentPane.add(btnNewButton_2_1);
		btnNewButton_2_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				ModelUI modelUI = new ModelUI(w);
				modelUI.setVisible(true);
				setVisible(false);
			}
		});
		
		
		JButton btnNewButton_2_2 = new JButton("添加患者");
		btnNewButton_2_2.setBounds(680, 132, 97, 23);
		contentPane.add(btnNewButton_2_2);
		btnNewButton_2_2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				AddPatient addPatient = new AddPatient(w);
				addPatient.setVisible(true);
				setVisible(false);
			}
		});
		
		table = new JTable(patientTable);
		table.setBounds(27, 177, 750, 237);
		contentPane.add(table);
		
		JButton btnNewButton_3 = new JButton("保存");
		btnNewButton_3.setBounds(134, 433, 97, 23);
		contentPane.add(btnNewButton_3);
		btnNewButton_3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				for(int i=0;i<patientTable.getRowCount();i++) {
					Patient p = new Patient();
					p.setBirthday((String)patientTable.getValueAt(i, 2));
					p.setCloseName((String)patientTable.getValueAt(i, 6));
					p.setClosePhone((String)patientTable.getValueAt(i, 7));
					p.setId((String)patientTable.getValueAt(i, 4));
					p.setName((String)patientTable.getValueAt(i, 1));
					p.setPhone((String)patientTable.getValueAt(i, 5));
					p.setSex((String)patientTable.getValueAt(i, 3));
					System.out.println(p);
					System.out.println(PatientDao.getInstance().cover(p));
				}
				JOptionPane.showMessageDialog(null, "保存成功");
			}
		});
		
		JButton btnNewButton_2_3 = new JButton("返回");
		btnNewButton_2_3.setBounds(509, 433, 97, 23);
		contentPane.add(btnNewButton_2_3);
		btnNewButton_2_3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Login login = new Login();
				login.setVisible(true);
				setVisible(false);
			}
		});
		
		//窗口居中
		setLocationRelativeTo(null);
		
		//加滚动条
		JScrollPane jp = new JScrollPane(table);
		jp.setBounds(27, 177, 750, 237);
		contentPane.add(jp);
		
		JLabel lblNewLabel = new JLabel("账号:"+w.getNumber()+" 姓名:"+w.getName()+" 生日:"+w.getBirthday()
		+" 专长:"+w.getKind()+" 职称:"+w.getJob()+" 密码:"+w.getPassword()+" 电话号:"+w.getPhone());
		lblNewLabel.setBounds(27, 67, 579, 44);
		contentPane.add(lblNewLabel);
		
		JButton btnNewButton_4 = new JButton("房屋管理");
		btnNewButton_4.setBounds(434, 132, 97, 23);
		contentPane.add(btnNewButton_4);
		btnNewButton_4.addActionListener((e)->{
			//要传入一个worker 返回时要用
			new RoomUI(w).setVisible(true);
			setVisible(false);
		});
		
		table.getColumnModel().getColumn(8).setCellEditor(new PatientButtonEditor(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            	int sum=0;
            	int tar=0;
            	for(int i=0;i<patientTable.getRowCount();i++) {
            		if((boolean)patientTable.getValueAt(i, 0)) {//选中了
            			tar = i;
            			sum++;
            		}
            	}
            	if(sum == 0) {
            		JOptionPane.showMessageDialog(null, "请选中要评估的患者");
            	}else if(sum >1){
            		JOptionPane.showMessageDialog(null, "请单选");
            	}else {
            		EvaluateUI evaluateUI = new EvaluateUI(w,PatientDao.getInstance().search(new Patient(((String)patientTable.getValueAt(tar, 4)))));
            		evaluateUI.setVisible(true);
            		setVisible(false);
            	}
            	
            }
        })); 
		table.getColumnModel().getColumn(8).setCellRenderer(new PatientButtonRender());
		
	}
}
